<template>
  <div class="app-container">
    <el-row>
      <el-col :span="23" :offset="1" style="margin-bottom: 10px;margin-top: 20px;">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/menu' }" class="bread"><i class="el-icon-location-outline"></i>系统管理</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/menu' }" class="bread">菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="7" :offset="17">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch">
          <el-form-item prop="menuName">
            <el-input
            class="inputem"
                v-model="queryParams.menuName"
                placeholder="请输入菜单名称"
                clearable
                @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>


    <el-row type="flex" justify="center">
      <el-col :span="22">
        <div class="mytable">
          <el-table height="500" stripe :data="menuList" class="tabletab" header-cell-class-name="table_header">
            <el-table-column
                type="selection"
                width="55">
            </el-table-column>
            <el-table-column label="菜单名称" prop="menuName"></el-table-column>
            <el-table-column label="菜单权限" prop="menuCode"></el-table-column>
            <el-table-column label="菜单类型" prop="menuType">
              <template slot-scope="scope">
                <span v-if="scope.row.menuType == 'C'">菜单</span>
                <span type="warning" v-else-if="scope.row.menuType == 'F'">按钮</span>
                <span type="danger" v-else>暂无分配</span>
              </template>
            </el-table-column>
            <el-table-column label="是否显示" prop="visible">
              <template slot-scope="scope">
                <el-tag effect="plain" type="danger"  v-if="scope.row.visible == 0">
                  是
                </el-tag>
                <el-tag effect="plain" type="info" v-if="scope.row.visible == 1">
                  否
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" prop="operate">
              <template slot-scope="scope">
                <el-button size="mini" v-if="scope.row.menuType == 'C'" @click="handleAdd(scope.row)" icon="el-icon-folder-add" plain>新增下级</el-button>
                <el-button size="mini" @click="handleUpdate(scope.row)" icon="el-icon-edit" plain>编辑</el-button>
                <el-button size="mini" @click="handleDelete(scope.row)" icon="el-icon-delete" plain>删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8" :offset="15">
        <div class="block" >
          <pagination
          class="pager"
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"
/>
        </div>
      </el-col>
    </el-row>


    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="740px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24" v-if="form.parentName != '-1'">
            <el-form-item label="上级菜单" prop="parentName">
              <el-input v-model="form.parentName"  :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单类型" prop="menuType">
              <el-radio-group v-model="form.menuType">
                <el-radio label="C">菜单</el-radio>
                <el-radio label="F">按钮</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="menuName">
              <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" type="number" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.menuType == 'C'">
            <el-form-item prop="component">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                组件路径</span>
              <el-input v-model="form.url" placeholder="请输入组件路径" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.menuType != 'M'">
            <el-form-item prop="perms">
              <el-input v-model="form.menuCode" placeholder="请输入权限标识" maxlength="100" />
              <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermi('system:user:list')`)" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                权限字符
              </span>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.menuType != 'F'">
            <el-form-item prop="status">
              <span slot="label">
                <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                菜单状态
              </span>
              <el-radio-group v-model="form.visible">
                <el-radio :label="0">显示</el-radio>
                <el-radio :label="1">不显示</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api//menu/menu";

export default {
  name: "Menu",
  data() {
    return {
      parent:undefined,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageSize:10,
        pageNum:1,
        menuName: undefined,
        visible: undefined
      },
      total:0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        menuName: [
          {required: true, message: "菜单名称不能为空", trigger: "blur"}
        ],
        orderNum: [
          {required: true, message: "菜单顺序不能为空", trigger: "blur"}
        ],
        path: [
          {required: true, message: "路由地址不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then(response => {
        this.menuList = response.rows
        this.loading = false;
        this.total = response.total
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listMenu().then(response => {
        this.menuOptions = [];
        const menu = {menuId: 0, menuName: '主类目', children: []};
        menu.children = this.handleTree(response.data, "menuId");
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset("",0);
    },
    // 表单重置
    reset(parentName,parent) {
      this.form = {
        parentName:parentName,
        menuId: undefined,
        parentId: parent,
        menuName: undefined,
        icon: undefined,
        menuType: "C",
        orderNum: undefined,
        visible: 0,
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //
      this.queryParams = {
        pageSize:10,
        pageNum:1
      }
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset(row.menuName,row.menuId)
      this.open = true;
      this.title = "添加菜单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset("-1",0);
      getMenu(row.menuId).then(response => {
        console.log(response)
        let data = response.data
        this.form = data;
        this.form.menuType = data.menuType
        this.form.parentName = '-1'
        this.open = true;
        this.title = "修改菜单";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.menuId != undefined) {
            updateMenu(this.form).then(response => {
              this.$model.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMenu(this.form).then(response => {
              this.$model.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      this.$model.confirm('是否确认删除名称为"' + row.menuName + '"的数据项？').then(function () {
        return delMenu(row.menuId);
      }).then(() => {
        this.getList();
        this.$model.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>

<style scoped>
.bread /deep/ .el-breadcrumb__inner{
  color: #000000;
}
.bread /deep/ .el-breadcrumb__inner:hover{color: #000000;}
.el-button{
  background-color: #4686D0;
  color: #ffffff;
  border: #4686D0;
  font-size: 12px;
  font-family: "微软雅黑";
}
.el-row{
  margin-top: 10px;
  /* margin-bottom:20px; */
}
.el-input{
  width:250px;
}
.inputem /deep/ .el-input__inner {
  background-color: rgba(0, 0, 0, 0) ;
  border:1px solid #B1B4C5;
}
.inputem /deep/ .el-input__inner:focus{
  border:2px solid #4c87c2;
}
.inputem /deep/ .el-input__inner:hover{
  border:2px solid #4c87c2;
}

.inputem /deep/ .el-input-group__append{
  background-color: #265389;
  border:1px solid #265389;
}
.el-icon-search{
  color: #ffffff;
}
/* .tabletab /deep/ .el-table__header-wrapper{
  background-color: #131627;
  font-family: "微软雅黑";
  font-size: 14px;
} */


/*.tabletab /deep/ .el-table__row{*/
/*  background-color: #131627;*/
/*  font-family: "微软雅黑";*/
/*  font-size: 14px;*/
/*}*/

/*.tabletab /deep/ .el-table__body-wrapper{*/
/*  background-color: #131627;*/
/*  font-family: "微软雅黑";*/
/*  font-size: 14px;*/
/*}*/

/* .el-table::before {
	 left: 0;
	 bottom: 0;

	 height: 0px;
} */

/*::v-deep .cell{*/
/*  color:#ffffff;*/
/*}*/
/*::v-deep.el-table tbody tr:hover > td {*/
/*  background-color: #2b3b5256 !important;*/

/*}*/
/*.tabletab /deep/ .table_header{*/
/*  background-color: #2b3b52 !important;*/
/*}*/
/*.mytable /deep/ .el-table__row>td{*/
/*  !* 去除表格线 *!*/
/*  border-bottom:1px solid #2B3B52;*/
/*}*/
/*.mytable /deep/ .el-table th.is-leaf {*/
/*  !* 去除上边框 *!*/
/*  border: none;*/
/*}*/

/*::v-deep .el-table::before{*/
/*  !* 去除下边框 *!*/
/*  !* height: 0; *!*/
/*  background-color: #2B3B52;*/
/*  !* height: 2px; *!*/
/*}*/
/*.mytable /deep/ .el-table th.el-table__cell{*/
/*  background-color: #2b3b52;*/
/*}*/
</style>
